package com.osi.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osi.bean.*;
import com.osi.mapper.AdminMapper;
import com.osi.mapper.CourseMapper;
import com.osi.service.AdminService;
import com.osi.service.CourseService;
import com.osi.service.StudentCourseService;
import com.osi.service.StudentService;
import com.osi.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lixin
 * @date 2021/2/8 16:13
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentCourseService studentCourseService;


    /**
     * 查询课程列表
     * @param course
     * @return
     */
    @Override
    public List<Course> queryList(Course course) {
        return baseMapper.selectList(new QueryWrapper<Course>()
                .like(StrUtil.isNotEmpty(course.getCourseName()), "course_name", course.getCourseName())
        );
    }


    @Override
    public List<Course> queryCourseList(Course course) {
        return baseMapper.selectList(new QueryWrapper<Course>()
                .eq("status", 1)
                .like(StrUtil.isNotEmpty(course.getCourseName()),
                        "course_name", course.getCourseName())
        );
    }

    /**
     * 学生查询已选课程
     * @return
     */
    @Override
    public List<Course> queryListByStudent() {
        Logininfo logininfo = UserContext.getCurreentUser();
        Student student = studentService.getOne(new QueryWrapper<Student>()
                .eq("username", logininfo.getUsername()));

        List<StudentCourse> studentCourseList = studentCourseService.list(new QueryWrapper<StudentCourse>()
                .eq("student_id", student.getId()));
        List<Integer> courseIds = studentCourseList.stream().map(StudentCourse::getCourseId).collect(Collectors.toList());

        List<Course> courseList = new ArrayList<>();
        if(courseIds.size() > 0) {
            courseList = baseMapper.selectBatchIds(courseIds);
        }

        return courseList;
    }

}
